package com.edu.buservice.util.model;

import com.edu.buservice.util.model.enums.ErrorCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-02-09 19:54
 **/

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R<T> {
	private static final long serialVersionUID = 1L;

	private String code;

	private boolean success;

	@JsonInclude(JsonInclude.Include.NON_NULL)
	private T data;

	private String msg;

	private R(String code, T data, String msg, boolean success) {
		this.code = code;
		this.data = data;
		this.msg = msg;
		this.success = success;
	}

	public R(String code, T data, String s) {
		this.code = code;
		this.msg = s;
		this.success = false;
		this.data = data;
	}


	/**
	 * 返回R
	 *
	 * @param data 数据
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> data(T data) {
		return data(data, "success");
	}

	/**
	 * 返回R
	 *
	 * @param data 数据
	 * @param msg  消息
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> data(T data, String msg) {
		return data("success", data, msg);
	}

	/**
	 * 返回R
	 *
	 * @param code 状态码
	 * @param data 数据
	 * @param msg  消息
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> data(String code, T data, String msg) {
		return new R<>(code, data, data == null ? "数据为空" : msg);
	}

	/**
	 * 返回R
	 *
	 * @param <T> T 泛型标记
	 * @return R
	 */
	public static <T> R<T> success(T data) {
		return new R<>("success", data, "success", true);
	}

	public static <T> R<T> success(T data, String msg) {
		return new R<>("success", data, msg, true);
	}


	/**
	 * 返回R
	 *
	 * @param code 状态码
	 * @param msg  消息
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(String code, String msg) {
		return new R<>(code, null, msg);
	}

	/**
	 * 返回R
	 *
	 * @param code 状态码
	 * @param msg  消息
	 * @param <T>  T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(String code, String msg, T data) {
		return new R<>(code, data, msg);
	}

	/**
	 * 返回R
	 *
	 * @param resultCode 业务代码
	 * @param <T>        T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(ErrorCodeEnum resultCode) {
		return new R<>(resultCode.getCode(), null, resultCode.getMessage());
	}

	/**
	 * 返回R
	 *
	 * @param resultCode 业务代码
	 * @param <T>        T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(ErrorCodeEnum resultCode, T data) {
		return new R<>(resultCode.getCode(), data, resultCode.getMessage());
	}

	/**
	 * 返回R
	 *
	 * @param resultCode 业务代码
	 * @param msg        消息
	 * @param <T>        T 泛型标记
	 * @return R
	 */
	public static <T> R<T> fail(ErrorCodeEnum resultCode, String msg) {

		return new R<>(resultCode.getCode(), null, msg);
	}

}
